package com.qianli.cashmama.trade.openApi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.utils.StringUtils;
import com.jsoniter.output.JsonStream;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.qianli.cashmama.trade.integration.product.ProductIntegrationService;
import com.qianli.cashmama.trade.openApi.controller.request.*;
import com.qianli.cashmama.trade.openApi.controller.response.OpenApiResult;
import com.qianli.cashmama.trade.openApi.controller.response.OpenApiResultCode;
import com.qianli.cashmama.trade.openApi.service.OpenApiService;
import com.qianli.cashmama.trade.openApi.utils.encryption.ReceiveData;
import com.qianli.cashmama.trade.openApi.utils.encryption.SendData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: cashmama
 * @Date: 2020/12/1-18:18
 * @Description: TODO
 * @Slogan: To be or not to be
 **/
@RestController
@ResponseBody
public class OpenApiTradeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OpenApiTradeController.class);
    @Autowired
    private OpenApiService openApiService;

    @Autowired
    private ProductIntegrationService productIntegrationService;

    /**
     * 风控回调，通过还是拒绝
     *
     * @param approveConclusionDTO
     * @return
     */
    @RequestMapping("/api.audit.result")
    public OpenApiResult riskCallBack(@RequestBody ReceiveData receiveData) {
        if (receiveData == null || StringUtils.isEmpty(receiveData.getMerchantCode()) || StringUtils.isEmpty(receiveData.getSign()) || StringUtils.isEmpty(receiveData.getBizData())) {
            return OpenApiResult.failure(OpenApiResultCode.FAILURE, "params invalid");
        }
        // TODO: 2021/10/11 记录下每家商户回调给我们的内容
        return openApiService.approveConclusion(receiveData);
    }

    @RequestMapping("/sign.check")
    public OpenApiResult checkSign(@RequestBody ReceiveData receiveData) {
        // TODO: 2021/10/11 记录下每家商户回调给我们的内容
        if (receiveData == null || StringUtils.isEmpty(receiveData.getMerchantCode()) || StringUtils.isEmpty(receiveData.getSign()) || StringUtils.isEmpty(receiveData.getBizData())) {
            return OpenApiResult.failure(OpenApiResultCode.FAILURE, "params invalid");
        }
        String merchantCode = receiveData.getMerchantCode();
        ProductCategory category = productIntegrationService.getCategoryByMerchantCode(merchantCode);
        if (category == null) {
            return OpenApiResult.failure(OpenApiResultCode.FAILURE, "product config fail");
        }

        String merchantPublicKey = category.getMerchantPublicKey();
        ApproveConclusionDTO approveConclusionDTO = JSONObject.parseObject(openApiService.getBizData(merchantPublicKey, receiveData), ApproveConclusionDTO.class);

        return OpenApiResult.success(JSON.toJSONString(approveConclusionDTO));
    }


    /**
     * 放款回调 放款成功还是失败  失败的原因是银行卡有问题
     *
     * @param repaymentPlanDetailDTO
     * @return
     */
    @RequestMapping("/api.loan.result")
    public OpenApiResult loanCallBack(@RequestBody ReceiveData receiveData) {
        if (receiveData == null || StringUtils.isEmpty(receiveData.getMerchantCode()) || StringUtils.isEmpty(receiveData.getSign()) || StringUtils.isEmpty(receiveData.getBizData())) {
            return OpenApiResult.failure(OpenApiResultCode.FAILURE, "params invalid");
        }
        return openApiService.repaymentPlan(receiveData);
    }


    /**
     * 还款回调
     *
     * @param repaymentStatusDTO
     * @return
     */
    @RequestMapping("/api.repayment.result")
    public OpenApiResult repaymentStatus(@RequestBody ReceiveData receiveData) {
        if (receiveData == null || StringUtils.isEmpty(receiveData.getMerchantCode()) || StringUtils.isEmpty(receiveData.getSign()) || StringUtils.isEmpty(receiveData.getBizData())) {
            return OpenApiResult.failure(OpenApiResultCode.FAILURE, "params invalid");
        }

        LOGGER.info("== 还款回调 ==,data:{}", JsonStream.serialize(receiveData));

        return openApiService.repaymentStatus(receiveData);
    }

    /**
     * 订单状态
     *
     * @param orderStatusDTO
     * @return
     */
    @RequestMapping("/api.order.status")
    public OpenApiResult orderStatus(@RequestBody ReceiveData receiveData) {
        if (receiveData == null || StringUtils.isEmpty(receiveData.getMerchantCode()) || StringUtils.isEmpty(receiveData.getSign()) || StringUtils.isEmpty(receiveData.getBizData())) {
            return OpenApiResult.failure(OpenApiResultCode.FAILURE, "params invalid");
        }
        return openApiService.orderStatus(receiveData);
    }


    /**
     * 获取订单状态
     *
     * @param
     * @return
     */
//    @RequestMapping("/gupta.order.status")
//    public OpenApiResult getOrderStatus(@RequestBody BaseRequest request) {
//        return openApiService.getOrderStatus(request);
//    }
//
//
//    /**
//     * 获取订单状态
//     *
//     * @param
//     * @return
//     */
//    @RequestMapping("/gupta.payment.plan")
//    public OpenApiResult getPaymentPlan(@RequestBody BaseRequest request) {
//        return openApiService.getPaymentPlan(request);
//    }


}
